<?php
	session_start();
	include "../include.php";
?>
<html>
<head>
<title><?php echo $SITE_TITLE ?> - Loot Search</title>
<link rel="stylesheet" type="text/css" href="../main.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Database Update</title>
</head>
<body>
<div id="container">
<div id="top">
<h1><?php echo $SITE_TITLE ?></h1>
</div>
<div id="leftnav">
</div>
<div id="content">
<strong>Please do not navigate away from this page a link appears to return to the home page.</strong><br>
<?php
	mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die(mysql_error());
	mysql_select_db(DB_NAME);
	if(get_db_version() >= 4) {
		if(!get_user_rights(RIGHTS_ADMIN)) {
			echo "You do not have permission to access this resource<br><a href=\"" . $SITE_URL . "\">Return to homepage</a>";
			complete_page_html();
			exit;
		}
	}
	if(get_db_version() < 2) {
		echo "updating database to version 2.<br>";
		$sql = "CREATE TABLE `general_info` (`site_version` VARCHAR( 20 ) NOT NULL ,`db_version` VARCHAR( 20 ) NOT NULL) TYPE = MYISAM ;";
		$result = mysql_query($sql);
		$sql = "ALTER TABLE `general_info` ADD `armory_updates` INT DEFAULT '1' NOT NULL ;";
		$result = mysql_query($sql);
		echo "     general info table created.<br>";
		$sql = "INSERT INTO `general_info` ( `site_version` , `db_version` ) VALUES ('0.37', '2');";
		$result = mysql_query($sql);
		echo "     initial general information imported into table general_info.<br>";
		echo "     adding unused item numbers to table items.<br>";
		for($x=1;$x<44810;$x++) {
			$sql = "SELECT * from items WHERE items_id = " . $x . ";";
			$result = mysql_query($sql);
			if(mysql_num_rows($result) < 1) {
				$sql = "INSERT into items (items_id,items_name,npcs_id,npcs_name,zone_name) VALUES(" . $x . ",'-99',-99,'-99','-99');";
				$result = mysql_query($sql);
			}
		}
		$sql = "ALTER TABLE `items` ADD `items_last_update` VARCHAR( 20 ) DEFAULT '1230524751' NOT NULL;";
		$result = mysql_query($sql);
		echo "<br>     items_last_update column added to table items.<br>";
		echo "version 2 of database upgrade complete.<br>";
	}
	if(get_db_version() < 3) {
		echo "updating database to version 3.<br>";
		$sql = "CREATE TABLE `users` (`users_id` INT NOT NULL AUTO_INCREMENT ,`users_login` VARCHAR( 20 ) NOT NULL ,`users_password` VARCHAR( 100 ) NOT NULL ,`users_salt` VARCHAR( 50 ) NOT NULL ,`users_rights` BIGINT NOT NULL ,UNIQUE (`users_id`)) TYPE = MYISAM ;";
		$result = mysql_query($sql);
		echo "     users table created.<br>";
		$salt = md5(uniqid(rand(), true));
		$salt = substr($salt, 0, 50);
		$sql = "INSERT into users (users_login,users_password,users_salt,users_rights) VALUES('admin','" . md5($salt."admin") . "','" . $salt . "',1);"; 
		$result = mysql_query($sql);
		$sql = "SELECT * from players;";
		$result = mysql_query($sql);
		while($myrow = mysql_fetch_row($result)) {
			$salt = md5(uniqid(rand(), true));
			$salt = substr($salt, 0, 50);
			$sql = "INSERT into users (users_login,users_password,users_salt,users_rights) VALUES('" . strtolower($myrow[1]) . "','" . md5($salt."temp") . "','" . $salt . "',0);";
			$result2 = mysql_query($sql);
		}
		echo "     users added for each player.<br>";
		echo "     admin user added, default password is admin.<br>";
		$sql = "UPDATE general_info set db_version='3';";
		$result = mysql_query($sql);
		echo "verion 3 of database upgrade complete.<br>";
	}
	if(get_db_version() < 4) {
		echo "updating database to version 4.<br>";
		$sql = "SELECT * from players;";
		$result = mysql_query($sql);
		echo "     updating players name to all lowercase.<br>";
		while($myrow = mysql_fetch_row($result)) {
			$new_name = strtolower($myrow[1]);
			$sql = "UPDATE players set players_name = '" . $new_name . "' WHERE players_id = " . $myrow[0] . ";";
			$result2 = mysql_query($sql);
		}
		$sql = "DROP TABLE `users`;";
		$result = mysql_query($sql);
		$sql = "CREATE TABLE `users` (`users_id` INT NOT NULL AUTO_INCREMENT ,`users_login` VARCHAR( 20 ) NOT NULL ,`users_password` VARCHAR( 100 ) NOT NULL ,`users_salt` VARCHAR( 50 ) NOT NULL ,`users_rights` BIGINT NOT NULL ,UNIQUE (`users_id`)) TYPE = MYISAM ;";
		$result = mysql_query($sql);
		echo "     users table created.<br>";
		$salt = substr(uniqid(rand(), true), 0, 8);
		$salt = md5($salt);
		$sql = "INSERT into users (users_login,users_password,users_salt,users_rights) VALUES('admin','" . md5($salt."admin") . "','" . $salt . "',3);";
		$result = mysql_query($sql);
		$sql = "SELECT * from players;";
		$result = mysql_query($sql);
		while($myrow = mysql_fetch_row($result)) {
			$salt = substr(uniqid(rand(), true), 0, 8);
			$salt = md5($salt);
			$sql = "INSERT into users (users_login,users_password,users_salt,users_rights) VALUES('" . strtolower($myrow[1]) . "','" . md5($salt."temp") . "','" . $salt . "',1);";
			$result2 = mysql_query($sql);
		}
		echo "     users added for each player.<br>";
		echo "     admin user added, default password is admin.<br>";
		$sql = "UPDATE general_info set db_version='4';";
		$result = mysql_query($sql);
		echo "verion 4 of database upgrade complete.<br>";
	}
	if(get_site_version() < 0.45) {
		$sql = "UPDATE general_info set site_version='0.45';";
		unlink("db_update.php");
		$result = mysql_query($sql);
		echo "Site updated to v0.45<br>";
	}
	if(get_site_version() < 0.49) {
		$sql = "UPDATE general_info set site_version='0.49';";
		$result = mysql_query($sql);
		echo "Site updated to v0.49<br>";
	}
?>
<a href="<?php echo $SITE_URL ?>">Return to home page</a>
</div>
<div id="footer">
<?php include "../footer.php" ?>
</div>
</div>
</body>
</html>